<?php
    class MainView
    {
        private $controller;
        private $model;
        private $counter;
        
        // Set a reference to the controller
        public function setController($controller)
        {
            $this->controller = $controller;
            $this->counter    = 0;
        }
        
        // The top HTML code
        public function startHTML()
        {
            echo '
                <html>
                    <head>
                        <title>DataStruct v0.1 Beta</title>		
                        <link rel="stylesheet" type="text/css" href="css/screen.css" />
                        <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
                        <script type="text/javascript" src="js/global.js"></script>
                    </head>
                    <body>
                        <div id="body">            
            ';
        }
        
        // The top
        public function showTop()
        {
            echo '
                <div id="top" class="panel">
                    <h1>datastruct v0.1 Beta</h1>
                    <div id="navigation">
                        <ul>
                            <li><a href="index.php?categories">Categories</a></li>
                            <li><a href="index.php?datatypes">Datatypes</a></li>
                            <li><a href="index.php?options">Options</a></li>
                            <li><a href="index.php?users">Users</a></li>
                            <li><a href="index.php?logout">Logout</a></li>
                        </ul>
                    </div>
                </div>
            ';
        }
        
        // The sidebar, containing the categories
        public function showSideBar()
        {
            echo '
                <div id="sidebar" class="panel">
                    <h2>Categories</h2>
            ';
            $this->showTree();
            echo '
                </div>
            ';
        }
        
        // The start of the content
        public function startContent()
        {
            echo '
    			<div id="content" class="panel">
            ';
        }
        
        // The category content
        public function showCategory($info)
        {
            echo '
                <h1>'.$info['name'].'</h1>
                <p>'.$info['description'].'</p>
            ';            
        }
        
        // Start the category form
        public function startForm($id_category)
        {
            echo '
                <form method="post" action="index.php?cat='.$id_category.'">
            ';
        }
        
        // Add / Edit form
        public function showForm($item) {
            echo '
                <input type="hidden" name="id" value="'.$item['id'].'" />
                <input type="hidden" name="action" value="'.$item['action'].'" />
                <input type="hidden" name="id_category" value="'.$item['id_category'].'" />
                <div>
					<label>Name:</label>
					<input class="text" type="text" name="name" value="'.$item['name'].'" />
				</div>
				<h3>Options</h3>
            ';
            foreach($item['options'] as $option) {
                if($option['hasmany']==0) {
                    $this->showOption($option);
                } else {
                    $this->showHasManyOption($option);
                }
            }
            echo '
                <label>&nbsp;</label>
                <input type="submit" value="Save" name="save" />
                <input type="hidden" name="manycounter" value="'.$this->counter.'" />
            ';
        }
        
        // Show a regular option
        public function showOption($option)
        {
            // TODO Show the options when they have the hasmany-attribute.
            
            // Set the name:
            $name = 'option_'.$option['id'];
            echo '
                <div>
                    <label>'.$option['description'].':</label>
            ';
            // Show the option input field:
            $this->showInputField($name, $option);
            // End this item:
            echo '</div>';
        }
        
        // Show an option that has the hasmany-attribute
        public function showHasManyOption($option)
        {
            echo '
                <div class="hasmany">
                    <label>'.$option['description'].':</label>
                    <ol>
            ';
            // Show an input field for each option:
            // TODO Fix this when 'add' is selected
            $tempOption = $option;
            foreach($option['value'] as $value) {
                echo '<li>';
                $tempOption['value'] = $value;
                // Set the name:
                $name = 'option_'.$option['id'].'_m_'.$this->counter;
                $this->counter++;
                // Show the option input field:
                $this->showInputField($name, $tempOption);
                echo '</li>';
            }
            
            // TODO Fix positioning of new item
            echo '
                    </ol>
                </div>
                <div>
                    <label>&nbsp;</label>
                    <a href="#" class="addAnother">Add another<var class="type">'.$option['type'].'</var><var class="optionID">'.$option['id'].'</var></a>
                </div>
            ';
        }
        
        // Show the input fields
        private function showInputField($name, $option)
        {
            switch($option['type']) {
                case 'textarea':
                {
                    // Textarea
                    echo '
                        <textarea rows="6" cols="40" name="'.$name.'">'.$option['value'].'</textarea>
                    ';
                    break;
                }
                case 'category' :
                {
                    // Category
                    // Show the items in this category:
                    $records = $this->controller->getRecords($option['id_category']);
                    if(count($records)==0) {
                        // There are no items in this category:
                        echo '
                            <select name="'.$name.'" disabled="disabled">
                                <option value="0">Category is empty</option>
                            </select>
                        ';
                    } else {
                        // Show the items in this category:
                        echo '
                            <select name="'.$name.'">
                        ';
                        foreach($records as $category) {
                            echo '<option value="'.$category['id'].'"';
                            if($category['id']==$option['value']) { echo ' selected="selected"'; }
                            echo '>'.$category['name'].'</option>';
                        }
                        echo '
                            </select>
                        ';
                    }
                    break;
                }
                case 'text' :
                case 'url' :
                case 'email':
                default:
                {
                    // Default text input
                    echo '
                        <input class="text" type="text" name="'.$name.'" value="'.$option['value'].'" />
                    ';
                    break;
                }
            }                
        }
        
        // Show the records
        public function showRecords($records)
        {
            echo '
                <table class="zebra optionlist">
                    <tr>
                        <th>&nbsp;</th>		
                        <th class="wide">Name</th>
                    </tr>	            
            ';
            foreach($records as $record) {
                echo '
                    <tr>
                        <td><input type="radio" name="id" value="'.$record['id'].'" /></td>
                        <td class="expandable"><span class="expand"><span class="arrow">+</span><var class="id">'.$record['id'].'</var></span>'.$record['name'].'</td>
                    </tr>
                ';
            }
            echo '
                </table>
            ';
        }
        
        public function showNoRecordsAvailable()
        {
            echo '
                <p><em>This category is empty</em></p>
            ';            
        }
        
        // Show Add/Edit/Delete buttons:
        public function showAddEditDelete() {
            echo '
                <input type="submit" name="edit" value="edit..." />
                <input type="submit" name="delete" value="delete" />
                <input type="submit" name="add" value="add new..." />            
            ';
        }        
        
        // End the category form
        public function endForm()
        {
            echo '
                </form>
            ';
        }
        
        // The end of the content
        public function endContent()
        {
            echo '
                </div>
            ';
        }
        
        // The footer
        public function showFooter()
        {
            echo '
                <div id="footer" class="panel">
                    <p>&copy; 2009 <a href="http://www.gielberkers.com">Giel Berkers</a></p>
                </div>
            ';
        }
        
        // The end of the HTML code
        public function endHTML()
        {
            echo '
                        </div>
                    </body>
                </html>            
            ';
        }
        
        // Show the categories in a tree
        private function showTree($id_category=0) {
            $categories = $this->controller->getCategories($id_category);
            if(count($categories)>0) {
                echo '<ul>';
                foreach($categories as $category) {
                    echo '<li><a href="index.php?cat='.$category['id'].'">'.$category['name'].'</a>';
                    $this->showTree($category['id']);
                    echo '</li>';
                }
                echo '</ul>';
        	}
        }
    }
?>